package sh.wnxy.wn38.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sh.wnxy.wn38.entity.User;
import sh.wnxy.wn38.service.IUserService;
import sh.wnxy.wn38.service.vo.LoginForm;
import sh.wnxy.wn38.service.vo.usc;
import sh.wnxy.wn38.utils.Result;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wn38
 * @since 2021-07-14
 */
@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {
    private IUserService us;

    @Autowired
    public void setUserroleService(IUserService us) {
        this.us = us;
    }

    @PostMapping("/denglu")
    public Result denglu(@RequestBody usc user) {
        QueryWrapper<User> ac = new QueryWrapper();
        if (!StringUtils.isEmpty(user.getUserName())) {
            ac.eq("user_name", user.getUserName());
        }
        if (!StringUtils.isEmpty(user.getUserPwd())) {
            ac.eq("user_pwd", user.getUserPwd());
        }
        User one = us.getOne(ac);
        return one != null ? Result.ok().msg("登陆成功") : Result.error().msg("用户名或密码错误");
    }

    @PostMapping("/zhuce")
    public Result zhuce(@RequestBody LoginForm loginForm) {
        QueryWrapper<User> ac = new QueryWrapper();
        if (!StringUtils.isEmpty(loginForm.getUserName())) {
            ac.eq("user_name", loginForm.getUserName());
            if (!(us.getOne(ac) == null)) {
                return Result.error().msg("用户名已存在");
            }
        }
        User user = new User();
//        赋值实体类相同字段
        BeanUtils.copyProperties(loginForm, user);
        return us.save(user) ? Result.ok().msg("注册成功") : Result.error().msg("网络波动,请重试");
    }

    @PostMapping("/xiugai")
    public Result xiugai(@RequestBody LoginForm loginForm) {
        User user = new User();
        BeanUtils.copyProperties(loginForm, user);
        user.setUserId("1415513079222910977");
        return us.updateById(user) ? Result.ok() : Result.error();
    }

}

